package edu.dqsy.mzq_translate;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.os.Handler;
import com.baidu.translate.demo.TransApi;

public class MainActivity extends AppCompatActivity {
    //百度翻译接口信息,id和key
    private static final String APP_ID = "20211109000994818";
    private static final String SECURITY_KEY = "Txi1cog0hYiX6htDsMAm";
//    TransApi api = new TransApi(APP_ID, SECURITY_KEY);
//
//    String query = "高度600米";
//        System.out.println(api.getTransResult(query, "auto", "en"));
    private Button b;
    private TextView t;
    private Handler handler = new Handler();
    private EditText e;
    String to = "en";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t = (TextView) findViewById(R.id.output);
        t.setText("");
        initView();
        Spinner spinner=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.language,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource
                (android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String [] lan = getResources().getStringArray(R.array.language);;
                switch (lan[position]){
                    case "中文":
                        to = "zh";
                        break;
                    case "英文":
                        to = "en";
                        break;
                    case "韩文":
                        to = "kor";
                        break;
                    case "日文":
                        to = "jp";
                        break;
                }
                System.out.println("设置目标语言:"+to);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    private void initView() {
        e = (EditText) findViewById(R.id.input);
        b = (Button) findViewById(R.id.tran);
        t = (TextView) findViewById(R.id.output);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                TransApi api = new TransApi(APP_ID, SECURITY_KEY);
                e = findViewById(R.id.input);
                t = findViewById(R.id.output);
                String s = e.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String resultJson = api.getTransResult(s, "auto", to);
                        System.out.println("目标语言:"+to);
                        System.out.println(resultJson);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                t.setText(resultJson);
                            }
                        });

                    }
                }).start();
            }
        });
    }

    public void tuichu(View v){
        Intent i = new Intent(MainActivity.this, denglu.class);
        startActivity(i);

    }
    public void danciben(View v){
        Intent i = new Intent(MainActivity.this, Danciben.class);
        startActivity(i);

    }
}